package com.knowledge.web.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.knowledge.model.pojo.Result;
import com.knowledge.model.pojo.UploadInfo;
import com.knowledge.common.utils.RedisTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/api/redis")
@Api(tags = "redis操作")
public class RedisController {

    @Autowired
    private RedisTools redisTools;

    @GetMapping("/test")
    @ApiOperation("redis测试")
    public Result redisTest() throws JsonProcessingException {
        UploadInfo uploadInfo = new UploadInfo();

        ObjectMapper mapper = new ObjectMapper();

        //添加Redis的基本信息
        redisTools.set("upload",mapper.writeValueAsString(uploadInfo));

        HashMap<String, Object> hashMap = new HashMap<>();

        hashMap.put("username","Liguanh");
        hashMap.put("age",32);

        redisTools.batchSet(hashMap);

        hashMap.put("love","吃饭");

        redisTools.batchSetIfAbsent(hashMap);

        return Result.success("成功测试");
    }
}
